package com.ruoyi.ruoyiyinzheng.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.ruoyiyinzheng.domain.vo.StMaterialCategeryVo;
import com.ruoyi.ruoyiyinzheng.domain.bo.StMaterialCategeryBo;
import com.ruoyi.ruoyiyinzheng.service.IStMaterialCategeryService;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 物料类别（树形)控制器
 * 前端访问路由地址为:/ruoyiyinzheng/materialCategery
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/materialCategery")
public class StMaterialCategeryController extends BaseController {

    private final IStMaterialCategeryService iStMaterialCategeryService;

    /**
     * 查询物料类别（树形)列表
     */
    @SaCheckPermission("ruoyiyinzheng:materialCategery:list")
    @GetMapping("/list")
    public R<List<StMaterialCategeryVo>> list(StMaterialCategeryBo bo) {
        List<StMaterialCategeryVo> list = iStMaterialCategeryService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出物料类别（树形)列表
     */
    @SaCheckPermission("ruoyiyinzheng:materialCategery:export")
    @Log(title = "物料类别（树形)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StMaterialCategeryBo bo, HttpServletResponse response) {
        List<StMaterialCategeryVo> list = iStMaterialCategeryService.queryList(bo);
        ExcelUtil.exportExcel(list, "物料类别（树形)", StMaterialCategeryVo.class, response);
    }

    /**
     * 获取物料类别（树形)详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ruoyiyinzheng:materialCategery:query")
    @GetMapping("/{id}")
    public R<StMaterialCategeryVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iStMaterialCategeryService.queryById(id));
    }

    /**
     * 新增物料类别（树形)
     */
    @SaCheckPermission("ruoyiyinzheng:materialCategery:add")
    @Log(title = "物料类别（树形)", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StMaterialCategeryBo bo) {
        return toAjax(iStMaterialCategeryService.insertByBo(bo));
    }

    /**
     * 修改物料类别（树形)
     */
    @SaCheckPermission("ruoyiyinzheng:materialCategery:edit")
    @Log(title = "物料类别（树形)", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StMaterialCategeryBo bo) {
        return toAjax(iStMaterialCategeryService.updateByBo(bo));
    }

    /**
     * 删除物料类别（树形)
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ruoyiyinzheng:materialCategery:remove")
    @Log(title = "物料类别（树形)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iStMaterialCategeryService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
